package com.etc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.entity.Employees;
import com.etc.entity.Records;
import com.etc.service.IRecordsService;
import com.etc.service.impl.RecordsServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;


@WebServlet("/RecordsController")
public class RecordsController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IRecordsService rs=new RecordsServiceImpl();
    
    public RecordsController() {
        super();
        // TODO Auto-generated constructor stub
    }

	
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		HttpSession session = request.getSession();
			if ("recordsByPage_ajax".equals(op)) {
				recordsByPage_ajax(request, response);
			} else if ("requestIndexRecords".equals(op)) {
				requestIndexRecords(request, response);
			} else  if ("queryAllRecordsId".equals(op)) {
				queryAllRecordsId(request, response,session);
			}
	}
    
    /**
	 * 请求首页所有考勤记录的方法
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void requestIndexRecords(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int normalNum = rs.countAllFullAttend(0);
		int delayNum = rs.countAllFullAttend(1);
		int earlyDepNum=rs.countAllFullAttend(2);
		int absentNum = rs.countAllFullAttend(3);
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(normalNum);
		list.add(delayNum);
		list.add(earlyDepNum);
		list.add(absentNum);
		//通过Ajax返回数据列表可以不用转json对象
/*		Gson gson = new Gson();
		String json = gson.toJson(list);*/
		System.out.println(list);
		response.getWriter().print(list);
		response.getWriter().close();
		
	}

	private void recordsByPage_ajax(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		//1.默认页码和显示条数
		int pageNo=1;//默认值
		int pageSize=5;//默认值

		//2.如果前台有指定值就使用前台的数据,否则按默认显示
		if(request.getParameter("pageNo")!=null) {
			pageNo=Integer.parseInt(request.getParameter("pageNo"));
		}
		if(request.getParameter("pageSize")!=null) {
			pageSize=Integer.parseInt(request.getParameter("pageSize"));
		}
		
		//调用业务层,返回分页类对象
		PageData pageData = rs.showAllRecordsByPage(pageNo, pageSize);
		
//		System.out.println(pageData.getData());
		//4.通过ajax返回json数据
		Gson gson = new Gson();
		String json = gson.toJson(pageData);
		response.getWriter().print(json);
		response.getWriter().close();
		
		
	}
	
	private void queryAllRecordsId(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		Employees emp= (Employees)session.getAttribute("emp");
		List<Records> list=rs.getqueryCurrentMonthRcordsById(emp.getEmpId());
		Gson gson=new Gson();
		String jsonString=gson.toJson(list);
		response.getWriter().print(jsonString);
		response.getWriter().close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
